/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dacnpm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author SONY
 */
public class Form_QuanLyCauHoi extends javax.swing.JFrame {
    String dapan;
    Statement stt;
    Connection conn;
    ResultSet rs;
    public Form_QuanLyCauHoi() throws Exception {
        initComponents();
        LoadDB();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta23_cauhoi = new javax.swing.JTextArea();
        jcb_da = new javax.swing.JComboBox();
        tf23_caua = new javax.swing.JTextField();
        tf23_cauc = new javax.swing.JTextField();
        tf23_caub = new javax.swing.JTextField();
        tf23_caud = new javax.swing.JTextField();
        bt23_addcauhoi = new javax.swing.JButton();
        bt23_logout = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(null);

        ta23_cauhoi.setColumns(20);
        ta23_cauhoi.setRows(5);
        jScrollPane1.setViewportView(ta23_cauhoi);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(190, 270, 400, 60);

        jcb_da.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcb_daItemStateChanged(evt);
            }
        });
        jPanel1.add(jcb_da);
        jcb_da.setBounds(356, 500, 120, 30);
        jPanel1.add(tf23_caua);
        tf23_caua.setBounds(290, 340, 320, 30);
        jPanel1.add(tf23_cauc);
        tf23_cauc.setBounds(290, 420, 320, 30);
        jPanel1.add(tf23_caub);
        tf23_caub.setBounds(290, 380, 320, 30);
        jPanel1.add(tf23_caud);
        tf23_caud.setBounds(290, 460, 320, 30);

        bt23_addcauhoi.setText("Them Cau Hoi");
        bt23_addcauhoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt23_addcauhoiActionPerformed(evt);
            }
        });
        jPanel1.add(bt23_addcauhoi);
        bt23_addcauhoi.setBounds(320, 540, 160, 40);

        bt23_logout.setText("Back");
        bt23_logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt23_logoutActionPerformed(evt);
            }
        });
        jPanel1.add(bt23_logout);
        bt23_logout.setBounds(710, 570, 55, 30);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dacnpm/mslide2_3.jpg"))); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(0, 0, 800, 600);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
public  void LoadDB() throws Exception{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url ="Jdbc:odbc:user";
		conn = DriverManager.getConnection(url);
		stt=conn.createStatement();
               String a="A",b="B",c="C",d="D";
                jcb_da.addItem(a);
                jcb_da.addItem(b);
                jcb_da.addItem(c);
                jcb_da.addItem(d);
                
                
                //System.out.print("THanh cong");
	}
public void add() throws SQLException{
    String sql ="INsert into question1 (name_question,question_a,question_b,question_c,question_d,answer)Values(?,?,?,?,?,?)";
    PreparedStatement addsql = conn.prepareStatement(sql);
    
    addsql.setString(1,ta23_cauhoi.getText());
    addsql.setString(2,tf23_caua.getText());
    addsql.setString(3,tf23_caub.getText());
    addsql.setString(4,tf23_cauc.getText());
    addsql.setString(5,tf23_caud.getText());
    addsql.setString(6,dapan);
    
    addsql.executeUpdate();
    
    
}
    private void bt23_addcauhoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt23_addcauhoiActionPerformed
        try {
            if(ta23_cauhoi.getText().isEmpty()||tf23_caua.getText().isEmpty()||tf23_caub.getText().isEmpty()||tf23_cauc.getText().isEmpty()||tf23_caud.getText().isEmpty())
            
                JOptionPane.showMessageDialog(rootPane, "Không được để trống bất cứ trường nào");
                
            else 
            {
            add();
            JOptionPane.showMessageDialog(rootPane, "Đã thêm câu hỏi!");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt23_addcauhoiActionPerformed

    private void bt23_logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt23_logoutActionPerformed
        this.setVisible(false);
        Form_QuanTri t = new Form_QuanTri();
        t.setVisible(true);
    }//GEN-LAST:event_bt23_logoutActionPerformed

    private void jcb_daItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcb_daItemStateChanged
        // TODO add your handling code here:
        dapan= jcb_da.getSelectedItem().toString();
    }//GEN-LAST:event_jcb_daItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Form_QuanLyCauHoi().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(Form_QuanLyCauHoi.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt23_addcauhoi;
    private javax.swing.JButton bt23_logout;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcb_da;
    private javax.swing.JTextArea ta23_cauhoi;
    private javax.swing.JTextField tf23_caua;
    private javax.swing.JTextField tf23_caub;
    private javax.swing.JTextField tf23_cauc;
    private javax.swing.JTextField tf23_caud;
    // End of variables declaration//GEN-END:variables
}
